export function hasClass (el, className) {
  let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  return reg.test(el.className)
}

export function addClass (el, className) {
  if (hasClass(el, className)) {
    return
  }

  let newClass = el.className.split(' ')
  newClass.push(className)
  el.className = newClass.join(' ')
}

export function setCookie (name, value, expires) {
  expires = expires || 300// 未传多少天则默认300天
  var expDays = expires * 24 * 60 * 60 * 1000
  var expDate = new Date()
  expDate.setTime(expDate.getTime() + expDays)
  var expString = expires ? ';expires=' + expDate.toGMTString() : ''
  document.cookie = name + '=' + encodeURI(value) + expString + ';path=/'
}
// 读取cookies
export function getCookie (name) {
  var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
  if (arr = document.cookie.match(reg)) {
    return decodeURI(arr[2])
  } else {
    return null
  }
}
// 删除cookies
export function delCookie (name) {
  var exp = new Date(new Date().getTime() - 1)
  var cval = getCookie(name)
  if (cval != null) {
    document.cookie = name + '=' + cval + ';expires=' + exp.toUTCString() + ';path=/'
  }
}

export function getData (el, name, val) {
  const prefix = 'data-'
  if (val) {
    return el.setAttribute(prefix + name, val)
  }
  return el.getAttribute(prefix + name)
}

let elementStyle = document.createElement('div').style

let vendor = (() => {
  let transformNames = {
    webkit: 'webkitTransform',
    Moz: 'MozTransform',
    O: 'OTransform',
    ms: 'msTransform',
    standard: 'transform'
  }

  for (let key in transformNames) {
    if (elementStyle[transformNames[key]] !== undefined) {
      return key
    }
  }

  return false
})()

export function prefixStyle (style) {
  if (vendor === false) {
    return false
  }

  if (vendor === 'standard') {
    return style
  }

  return vendor + style.charAt(0).toUpperCase() + style.substr(1)
}

export function RandomString (length) {
  var str = ''
  for (; str.length < length; str += Math.random().toString(36).substr(2));
  return str.substr(0, length)
}

export function maskCallback (callback, btnCls, btnCls2) {
  let $body = document.getElementsByTagName('body')[0]
  $body.addEventListener('click', function (e) {
    let $node = e.target
    if ($node.className.indexOf(btnCls) != -1) {
      $node.parentNode.parentNode.parentNode.remove()
      callback && callback()
    }
    if ($node.className.indexOf(btnCls2) != -1) {
      $node.parentNode.parentNode.parentNode.remove()
    }
  }, false)
}

export function maskTips (opts) {
/*
* _____H5弹窗组件____
* ----opts{对象}参数----
* @param id {string}
* @param txt {string}
* @param title {string}
* @param intro {string}
* @param btnTxt {string}
* @param btnTxt2 {string}
* @param btnCls {string}
* @param btnCls2 {string}
*/

  var $mask = document.createElement('div')
  $mask.id = opts.id ? 'mask_h5_' + opts.id : 'mask_h5_' + String(Math.random()).replace('.', '_')
  $mask.className = 'component-mask'
  var count = 0
  for (var attr in opts) {
    if (opts.hasOwnProperty(attr)) {
      count++
    }
  }
  if (count <= 2 && opts.txt) {
    var ID = 'mask_h5_' + opts.id
    if (document.getElementById(ID)) {
      if (document.getElementById(ID).style.display == 'none') {
        document.getElementById(ID).style.display = 'block'
        setNone(ID)
      }
    } else {
      $mask.style.cssText = 'width:80%;height:auto;z-index:9999;position:fixed;left:50%;top:50%;background-color:rgba(0,0,0,.6);color:#fff;font-size:15px;padding:15px;border-radius:20px;line-height:22px;text-align:center;transform:translate(-50%,-50%);-webkit-transform:translate(-50%,-50%);-moz-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);-o-transform:translate(-50%,-50%);'
      $mask.innerHTML = opts.txt
      opts.parentElem ? opts.parentElem.appendChild($mask) : document.getElementsByTagName('body')[0].appendChild($mask)
      setNone(ID)
    }
  } else {
    if (document.getElementById('mask_h5_' + opts.id)) {
      document.getElementById('mask_h5_' + opts.id).style.display = 'block'
      return
    }
    var $maskC = document.createElement('div')
    var $p1 = document.createElement('p')
    $maskC.className = 'component-mask-c'
    $p1.className = 'component-info-mask'
    $mask.style.cssText = 'width:100%;height:100%;z-index:9999;position: fixed;left:0;top:0;right:0;bottom:0;background-color:rgba(0,0,0,.6);text-align:center;'
    $maskC.style.cssText = 'width:80%;height:auto;position:fixed;left:50%;top:50%;background-color:#fff;color:#333;font-size:12px;border-radius:15px;padding:15px;transform:translate(-50%,-50%);-webkit-transform:translate(-50%,-50%);-moz-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);-o-transform:translate(-50%,-50%);'
    $p1.style.cssText = 'width:100%;height:auto;line-height:22px;font-size:14px;padding:15px 0;'
    if (opts.title) {
      var $h2 = document.createElement('h2')
      $h2.className = 'component-title-mask'
      $h2.style.cssText = 'height:35px;line-height:22px;text-align:center;font-size:16px;color:#333;width:100%;overflow:hidden;'
      $h2.innerHTML = opts.title
      $maskC.appendChild($h2)
    }
    $maskC.appendChild($p1)
    if (opts.txt) {
      $p1.innerHTML = opts.txt
    }
    if (opts.intro) {
      var $p2 = document.createElement('p')
      $p2.innerHTML = opts.intro
      $p2.className = 'component-intr-mask'
      $p2.style.cssText = 'width:100%;height:auto;line-height:22px;padding:10px 0;'
      $maskC.appendChild($p2)
    }
    if (opts.btnTxt) {
      var $btncnt = document.createElement('div')
      $btncnt.className = 'component-btn'
      $btncnt.style.cssText = 'width:100%;height:auto;padding:25px 0 10px 0;overflow: hidden;text-align:center;font-size:15px;'
      var $btnLeft = document.createElement('a')
      $btnLeft.setAttribute('href', 'javascript:;')
      $btnLeft.className = 'comp-mask-btnAleft'
      if (opts.btnCls) {
        $btnLeft.className = 'comp-mask-btnAleft ' + opts.btnCls
      }
      $btnLeft.innerHTML = opts.btnTxt
      $btnLeft.style.cssText = 'display:inline;padding:10px 20px;border-radius:15px;border:1px solid #999;margin:0 10px;color:#333;'
      $btncnt.appendChild($btnLeft)
      $maskC.appendChild($btncnt)
    }
    if (opts.btnTxt2) {
      var $btnRight = document.createElement('a')
      $btnRight.innerHTML = opts.btnTxt2
      $btnRight.setAttribute('href', 'javascript:;')
      $btnRight.className = 'comp-mask-btnAright'
      if (opts.btnCls2) {
        $btnRight.className = 'comp-mask-btnAright ' + opts.btnCls2
      }
      $btnRight.style.cssText = 'display:inline;padding:10px 20px;border-radius:15px;border:1px solid #999;margin:0 10px;color:#333;'
      $btncnt.appendChild($btnRight)
    }
    $mask.appendChild($maskC)
    opts.parentElem ? opts.parentElem.appendChild($mask) : document.getElementsByTagName('body')[0].appendChild($mask)
  }
  function setNone (id) {
    setTimeout(function () {
      document.getElementById(id).style.display = 'none'
    }, 2000)
  }
}
